﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxCallback;
using TireService.Data;
using System.Web.Security;
using System.Configuration;

namespace TireService.Web.UserControls
{
    public partial class ucEditTireSetPattern : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void callbackEditTireSetPatternPanel_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            LoadUserControl(Convert.ToInt32(e.Parameter));
        }

        /// <summary>
        /// Public function for loading usercontrol
        /// </summary>
        public void LoadUserControl(int TireSetId)
        {
            // Show usercontrol edit form
            pEditTireSet.Visible = true;

            // Initiate datalayer
            TireServiceDB db = new TireServiceDB();


            if (TireSetId != 0)
            {
                LoadTireSet(TireSetId, db);
            }
            else
            {
                // Error
                throw new ArgumentException("Hjulsæt ikke fundet");
            }
        }

        /// <summary>
        /// Function that load tireset and set edit form values
        /// </summary>
        /// <param name="TireSetId">Id on tireset to loadTireSet</param>
        /// <param name="db">Connection to database</param>
        private void LoadTireSet(int TireSetId, TireServiceDB db)
        {
            // Fetch tiresetId
            TireSet tireset = db.GetTireSet(TireSetId);

            // Set values in edit form
            if (tireset != null)
            {
                txtLeftFrontPattern.Text = tireset.LeftFrontTirePattern.ToString();
                txtLeftRearPattern.Text = tireset.LeftRearTirePattern.ToString();
                txtRightFrontPattern.Text = tireset.RightFrontTirePattern.ToString();
                txtRightRearPattern.Text = tireset.RightRearTirePattern.ToString();
            }
            else
            {
                // Error
                throw new ArgumentException("Hjulsæt kunne ikke loades");
            }
        }

        /// <summary>
        /// Callback function that saves tireset
        /// </summary>
        protected void callBackSaveTireSetPattern_Callback(object sender, CallbackEventArgs e)
        {
            try
            {
                // Get values from client
                string[] param = e.Parameter.Split('|');

                int TireSetId = Convert.ToInt32(param[0]);

                // Initiate datalayer
                TireServiceDB db = new TireServiceDB();

                TireSet tireset = new TireSet();

                if (TireSetId == 0)
                {
                    // Error
                    throw new ArgumentException("Hjulsæt ikke fundet");
                }
                else
	            {
                    // Load tireset
                    tireset = db.GetTireSet(TireSetId);
	            }

                tireset.LeftFrontTirePattern = Convert.ToDecimal(param[1]);
                tireset.RightFrontTirePattern = Convert.ToDecimal(param[2]);
                tireset.LeftRearTirePattern = Convert.ToDecimal(param[3]);
                tireset.RightRearTirePattern = Convert.ToDecimal(param[4]);

                // Get UserId
                Guid UserId = new Guid(Membership.GetUser().ProviderUserKey.ToString());

                // Create tireset log
                TireSetLogBLL tiresetBLL = new TireSetLogBLL();
                tiresetBLL.CreateTireSetLog(db, tireset, DateTime.Now, tireset.TireSetStatusId, tireset.TireSetStatusId, UserId, "Opdatering af dækmønster", tireset.LeftFrontTirePattern, tireset.RightFrontTirePattern, tireset.LeftRearTirePattern, tireset.RightRearTirePattern, tireset.ContainerCargoRoomId);

                // Commit changes to database
                db.SubmitChanges();

                // Check if email should be send to customer
                if (Convert.ToBoolean(param[5]))
                {
                    // Send email to customer
                    EmailNotificationServices emailService = new EmailNotificationServices();

                    Car car = db.GetCar(tireset.CarId);

                    // Send email to customer with tire set pattern witth user on bcc
                    emailService.TireSetPattern_SendCustomerEmail(car.CustomerId, db.GetCustomerEmail(car.CustomerId), db.GetCustomerName(car.CustomerId), car.RegistrationNumber, tireset, db);
                }

                // Dispose database connection
                db.Dispose();
            }
            catch (Exception ex)
            {
                // Error
                throw new ArgumentException("Fejl opstod: " + ex.Message);
            }
        }

    }
}